package com.guaoran.webmvc.advice;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : gucheng
 * @Description : <br/>
 * @date :2018/11/13 9:34
 */
@RestControllerAdvice
public class DemoRestControllerAdvicer {
    /**
     * 采用MVC方式对异常进行捕获处理
     * @param throwable
     * @return
     */
    @ExceptionHandler(value = {
            NullPointerException.class,
            IndexOutOfBoundsException.class
    })
    public Object handleException(
            Throwable throwable) {
        Map<String,Object> data = new HashMap<>();
        data.put("message",throwable.getMessage());
        data.put("code",throwable.getLocalizedMessage());
        return data;
    }
}
